package cryptage;

import DesInterface.DesPanel;

/**
 * Classe Permutation
 * @author marie, margaux
 */
class Permutation {	

	/**
	 * Vecteur de permutation contenant les scores
	 */
    private int[] permutationScores = new int[64];
    
    /**
     * Table de permutation initiale
     */
    private int permutationTab[][] = {{58, 50, 42, 34, 26, 18, 10, 2},
    								  {60, 52, 44, 36, 28, 20, 12, 4},
    								  {62, 54, 46, 38, 30, 22, 14, 6},
    								  {64, 56, 48, 40, 32, 24, 16, 8},
    								  {57, 49, 41, 33, 25, 17,  9, 1},
    								  {59, 51, 43, 35, 27, 19, 11, 3},
    								  {61, 53, 45, 37, 29, 21, 13, 5},
    								  {63, 55, 47, 39, 31, 23, 15, 7}};
    
    /**
     * Table de permutation finale : inverse de la table de permutation initiale
     */
    private int inversePermutationTab[][] = {{40, 8, 48, 16, 56, 24, 64, 32},
    										 {39, 7, 47, 15, 55, 23, 63, 31},
    										 {38, 6, 46, 14, 54, 22, 62, 30},
    										 {37, 5, 45, 13, 53, 21, 61, 29},
    										 {36, 4, 44, 12, 52, 20, 60, 28},
    										 {35, 3, 43, 11, 51, 19, 59, 27},
    										 {34, 2, 42, 10, 50, 18, 58, 26},
    										 {33, 1, 41,  9, 49, 17, 57, 25}};
    
    /**
     * 
     */
    private String permS[][] = new String[8][8];
    
    /**
     * 
     */
    private String newBlock64[][] = new String[4][8];
    
    
    /**
     * Constructeur : cree une instance de Permutation
     */
    public Permutation() {
        
    }
    
    /**
     * Remplissage des scores de permutation initiale, passant ainsi d'un tableau a un vecteur
     */
    public void fillPermutation() {
        int index = 0;
        
        for (int row = 0; row < 8; row++) {
        	for (int col = 0; col < 8; col++) {
        		permutationScores[index] = permutationTab[row][col];
        		index++;
            }
        }
    }
    
    /**
     * Remplissage des scores de permutation finale, passant ainsi d'un tableau a un vecteur
     */
    public void fillInversePermutation() {
        int index = 0;
        
        for(int row = 0; row < 8; row++) {
            for(int col = 0; col < 8; col++) {
            	permutationScores[index] = inversePermutationTab[row][col];
            	index++;
            }
        }   
    }
    
    /**
     * Methode permettant de faire la permutation initiale
     * @param perm_in le vecteur de permutation d'entree
     * @param perm_out le vecteur de permutation de sortie
     */
    public void doPermutation(int[] perm_in, int[] perm_out) {
        int temp = 0;
        int i = 0;
        int loop = 0;
        int check = 0;
         
        while (perm_in.length != check) {
            temp = permutationScores[i];
            if (temp == loop) {
                perm_out[check] = perm_in[loop - 1];
                loop = 0;
                check++;
                i++;
            }
            loop++;
        }
        
        System.out.println("La sortie permutee");
        for (int j = 0; j < perm_out.length; j++) {
            System.out.print(perm_out[j]);
        }
        
        int index = 0;
        for (int g = 0; g< 8; g++) {
            for (int j = 0; j < 8; j++) {
                permS[g][j] = Integer.toString(perm_in[index]);
                index++;
            }
        }
        
        DesPanel.printArray(permS, "Apres la permutation initiale");
    }
    
    /**
     * Methode permettant de faire la permutation finale, c'est-a-dire inverse
     * @param perm_in le vecteur de permutation d'entree
     * @param perm_out le vecteur de permutation de sortie
     */
    public void doInversePermutation(int[] perm_in, int[] perm_out) {
        int temp = 0;
        int i = 0;
        int loop = 0;
        int check = 0;
        
        while (perm_in.length != check) {
            temp = permutationScores[i];
            if (temp == loop) {
                perm_out[check] = perm_in[loop-1];
                loop = 0;
                check++;
                i++;
            }
            loop++;
        }
        
        int index = 0;
        for (int d = 0; d < 4;d++) {
            for (int j = 0; j < 8; j++) {
                newBlock64[d][j] = Integer.toString(perm_out[index]);                 
                index++;
            }
        }
        
        DesPanel.printArray(newBlock64, "Apres IP");
    }
}